<?php
namespace app\admin\controller;

use think\Controller;

class City extends Controller
{

    public function index()
    {
        $parentId = input('get.parent_id',0,'intval');
        $citylist = model("City")->getFirstCity($parentId);

        return $this->fetch('',[
            'citylist'=> $citylist,

        ]);


    }

    public function edit($id=0) {
        if(intval($id) < 1) {
            $this->error("参数不合法");
        }
       $city = model("City")->get($id);
       $citylist = model("City")->getNormalFirstCity();

        return $this->fetch('',[
            'city' => $city,
            'citylist'=> $citylist
        ]);



    }  
    public function add() {
         $citylist = model("City")->getNormalFirstCity();
        return $this->fetch('',[
            'citylist' => $citylist
        ]);
    } 
    public function listorder($id,$listorder) {
             
       $res = model("City")->save(['listorder'=>$listorder],['id'=>$id]);
       if ($res) {
        $this->result($_SERVER['HTTP_REFEER'],1,'succeess');
        }  else {
            $this->result($_SERVER['HTTP_REFEER'],1,'failure');
 
        }
    } 

    public function save() {
        $data = input('post.');


         // edit save
         if (!empty($data['id'])) {
           $res = model('City')->update($data);

           if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }



         } else {
          // add new 
           $res = model('City')->add($data);
           if ($res) {
            $this->success('新增成功');
        } else {
            $this->error('新增失败');
        }
         }
   
        


    }    




}
